﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Principal;
using System.Data;

namespace StonePonyMVC.StonePony.Classes
{
    public enum StonePonyRoles { customer = 1, maintenance = 2, administrator = 3 }

    public class StonePonyIdentity : IIdentity
    {
        #region IIdentity Members

        public string AuthenticationType
        {
            get { return "Forms"; }
        }

        public bool IsAuthenticated { get; internal set; }

        public string Name
        {
            get { return (this.UserFirstname + ' ' + this.UserLastName).Trim(); }
        }

        #endregion

        /// <summary>
        /// user_email
        /// </summary>
        public string UserEmail { get; private set; }

        /// <summary>
        /// user_password
        /// </summary>
        public string UserPassword { get; private set; }
        
        /// <summary>
        /// user_firstname
        /// </summary>
        public string UserFirstname { get; private set; }

        /// <summary>
        /// user_lastname
        /// </summary>
        public string UserLastName  { get; private set; }

        /// <summary>
        /// user_company
        /// </summary>
        public string UserCompany { get; private set; }

        /// <summary>
        /// user_phone_1, user_phone_2, user_phone_3
        /// </summary>
        public string[] UserPhones { get; private set; }

        /// <summary>
        /// user_role
        /// </summary>
        public StonePonyRoles UserRole { get; private set; }

        /// <summary>
        /// user_last_login
        /// </summary>
        public DateTime? UserLastLogin  { get; private set; }

        public StonePonyIdentity()
        {
            this.IsAuthenticated = false;
            this.UserPhones = new string[3] { null, null, null };
            UserRole = StonePonyRoles.customer;
        }

        public StonePonyIdentity(IDataRecord rc)
        {
            Func<string, string> getStringValue = delegate(string fldName)
            {
                int fldPosition = rc.GetOrdinal(fldName);
                return rc.IsDBNull(fldPosition) ? String.Empty : rc.GetString(fldPosition);
            };
            Func<string, int> getIntValue = delegate(string fldName)
            {
                int fldPosition = rc.GetOrdinal(fldName);
                return rc.IsDBNull(fldPosition) ? 1 : rc.GetInt32(fldPosition);
            };
            Func<string, DateTime?> getDateTimeValue = delegate(string fldName)
            {
                int fldPosition = rc.GetOrdinal(fldName);
                if (rc.IsDBNull(fldPosition))
                    return null;
                else
                    return  rc.GetDateTime(fldPosition);
            };

            this.UserEmail = getStringValue("user_email");
            this.UserPassword = getStringValue("user_password");
            this.UserFirstname = getStringValue("user_firstname");
            this.UserLastName = getStringValue("user_lastname");
            this.UserCompany = getStringValue("user_company");
            this.UserPhones = new string[3] { getStringValue("user_phone_1"), getStringValue("user_phone_2"), getStringValue("user_phone_3") };
            
            int tmp = getIntValue("user_role");
            if (tmp > 0 && tmp <= 3)
                this.UserRole = (StonePonyRoles)Enum.ToObject(typeof(StonePonyRoles), tmp);
            else
                this.UserRole = StonePonyRoles.customer;

            this.UserLastLogin = getDateTimeValue("user_last_login");
            this.IsAuthenticated = false;
        }

        public bool IsInRole(string role)
        {
            StonePonyRoles sp_rl;
            if (!Enum.TryParse<StonePonyRoles>(role, true, out sp_rl))
                return false;
            
            return sp_rl == this.UserRole;
        }
    }
}